package com.iknow.xmpp.smack.avatar;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileAvatarCache
  implements AvatarCache
{
  private File mStoreDir;

  public FileAvatarCache(File paramFile)
  {
    if ((paramFile.exists()) && (!(paramFile.isDirectory())))
      throw new IllegalArgumentException("The store directory must be a directory");
    this.mStoreDir = paramFile;
    this.mStoreDir.mkdirs();
  }

  public boolean contains(String paramString)
  {
    return new File(this.mStoreDir, paramString).exists();
  }

  public byte[] get(String paramString)
    throws IOException
  {
    BufferedInputStream localBufferedInputStream = new BufferedInputStream(new FileInputStream(new File(this.mStoreDir, paramString)));
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      byte[] arrayOfByte = new byte[1024];
      localBufferedInputStream.read(arrayOfByte);
      localByteArrayOutputStream.write(arrayOfByte);
      return localByteArrayOutputStream.toByteArray();
    }
    finally
    {
      localBufferedInputStream.close();
    }
  }

  public void put(String paramString, InputStream paramInputStream)
    throws IOException
  {
    byte[] arrayOfByte;
    int i;
    BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(this.mStoreDir, paramString)));
    try
    {
      arrayOfByte = new byte[1024];
      i = paramInputStream.read(arrayOfByte);
      if (i == -1)
      {
        paramInputStream.close();
        localBufferedOutputStream.close();
      }
    }
    finally
    {
      paramInputStream.close();
      localBufferedOutputStream.close();
    }
  }

  public void put(String paramString, byte[] paramArrayOfByte)
    throws IOException
  {
    BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(this.mStoreDir, paramString)));
    try
    {
      localBufferedOutputStream.write(paramArrayOfByte);
      return;
    }
    finally
    {
      localBufferedOutputStream.close();
    }
  }
}